package com.ly.count.exception;

import cn.hutool.json.JSONUtil;
import com.ly.count.http.CPlusResponse;
import com.ly.count.http.CplusHttpStatus;
import com.ly.count.utils.ResponseModelUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.ly.count.http.CPlusResponse;
import org.apache.tomcat.util.http.ResponseUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.resource.NoResourceFoundException;

@ControllerAdvice
public class GlobalExceptionHandler {
    Logger logger = (Logger) LogManager.getLogger(GlobalExceptionHandler.class);
    @ExceptionHandler(value = {IllegalArgumentException.class})
    @ResponseBody
    public CPlusResponse<CplusHttpStatus> handleIllegalArgumentException(IllegalArgumentException ex, WebRequest request) {
        logger.info("IllegalArgumentException:{}",ex);
        return ResponseModelUtils.fail(CplusHttpStatus.APP_PARAM_ERROR);
    }

    @ExceptionHandler(value = {Exception.class})
    @ResponseBody
    public CPlusResponse<CplusHttpStatus> handleGeneralException(Exception ex, WebRequest request) {
        logger.info("Exception:{}",ex);
        return ResponseModelUtils.fail(CplusHttpStatus.APP_SERVICE_ERROR);
    }
}
